package co.com.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import co.com.ejb.WineSearchLocal;
import co.com.ejb.stateful.ShoppingCartBeanLocal;

/**
 * Servlet implementation class ShoppingCart
 */
@WebServlet("/ShoppingCart")
public class ShoppingCartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@EJB
	ShoppingCartBeanLocal bean;
	
    /**
     * Default constructor. 
     */
    public ShoppingCartServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if(action.equalsIgnoreCase("addItem")){
			String item = request.getParameter("item");
			bean.addItem(item);
		} else if(action.equalsIgnoreCase("closeSession")){
			bean.closeSession();
		} else if(action.equalsIgnoreCase("getItems")){
			List<String> items = bean.getItems();
			response.getWriter().write(items);
		} else {
			response.getWriter().write("Hello " + Thread.currentThread().getName());
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
